package cn.xawl.dao;

import cn.xawl.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author lh
 * @date 2020/01/25 18:14
 *
 * 符合SpringDataJpa的dao层接口规范要继承JpaRepository<操作的实体类类型，实体类中主键属性的类型>  -->封装了基本CRUD操作
 *                                   和JpaSpecificationExecutor<操作的实体类类型>  -->封装了复杂查询（分页）
 *
 **/

public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {

    /**
     * 根据客户名称查询客户
     *      使用jpql形式查询
     *      jpql：from Customer where custName = ?
     */
    @Query(value = "from Customer where custName = ?1 ")
    public Customer findJpql(String custName);

    /**
     * 根使用jpql完成更新操作
     *      更新客户的名称,将名称改为黑马程序员
     *
     * sql：update cst_customer set cust_name = ? where cust_id = ?
     * jpql:update Customer set custName = ? where custId = ?
     *
     * Query注解代表进行查询
     *      声明此方法是用来进行更新操作
     *Modifying注解代表这是一个更新操作
     *
     */
    @Query("update Customer set custName = ?2 where custId = ?1")
    @Modifying
    public void updateCustomerById(Long custId, String custName);


    /**
     * 使用Sql查询
     *      查询全部客户
     *      sql:select *from cst_customer
     * Query注解：配置sql查询
     *      value:sql语句
     *      nativeQuery：是否使用jpql
     *          true：sql
     *          false：jpql
     */

//    @Query(value = "select *from cst_customer", nativeQuery = true)
    @Query(value = "select *from cst_customer where cust_name like ?1", nativeQuery = true)
    public List<Object []> findAllBySql(String name);


    /**
     * 使用客户名称模糊匹配和客户所属行业精准匹配的查询
     */
    @Query(value = "select *from cst_customer where cust_name like ?1 and cust_industry = ?2", nativeQuery = true)
    public Customer findByCustNameLikeAndCustIndustry(String custName, String custIndustry);





}
